package com.uam.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uam.db.mapper.SectorStatMapper;
import com.uam.db.mapper.TaskMapper;
import com.uam.db.mapper.TaskStatMapper;
import com.uam.db.model.bo.SectorStatCacheModel;
import com.uam.db.model.dto.TaskListDTO;
import com.uam.db.model.entity.SectorStat;
import com.uam.db.model.entity.TaskStat;
import com.uam.db.model.vo.SectorStatVo;
import com.uam.service.HomeService;
import com.uam.utils.RedisUtil;
import com.uam.web.common.model.PageRes;
import com.uam.web.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author uam
 * @create 2023/12/5 15:02
 * @email uam@uam.com
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private TaskStatMapper taskStatMapper;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private SectorStatMapper sectorStatMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Result<List<TaskStat>> groupBystatus() {
        LambdaQueryWrapper<TaskStat> taskQueryWrapper = new LambdaQueryWrapper<>();
        return Result.createSuccess(taskStatMapper.selectList(taskQueryWrapper));
    }

    @Override
    public Result<PageRes<TaskListDTO>> selectTaskList(Integer pageNum, Integer pageSize, List status) {

        Page<TaskListDTO> page = new Page<>(pageNum, pageSize);
        IPage<TaskListDTO> iPage = taskMapper.selectTaskList(page, status, null, null);
        PageRes<TaskListDTO> taskListDTOPageRes = new PageRes<>(iPage.getRecords(), iPage.getTotal());
        return Result.createSuccess(taskListDTOPageRes);
    }

    @Override
    public Result<List<SectorStatVo>> sectorList() {
        List<SectorStatVo> list = new ArrayList<>();
        String sectorPoints =redisUtil.get("SECTOR_POINTS");
        if(sectorPoints != null){
            List<SectorStatCacheModel>  models=JSONArray.parseArray(sectorPoints, SectorStatCacheModel.class);
            models.forEach(src -> {
                SectorStatVo sectorStatVo = new SectorStatVo();
                sectorStatVo.setName(src.getName());
                sectorStatVo.setId(src.getId());
                sectorStatVo.setTime(src.getTime());
                sectorStatVo.setNum(src.getNum()+"");
                sectorStatVo.setCapacity(src.getCapacity());
                sectorStatVo.setAircraftNames(JSON.parseArray(src.getAircraftNames()));
                list.add(sectorStatVo);
            });
            return Result.createSuccess(list);
        }
        String defaultData="[{\"id\":\"112430102000000\",\"name\":\"芙蓉区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371},{\"id\":\"112430103000000\",\"name\":\"天心区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371},{\"id\":\"112430104000000\",\"name\":\"岳麓区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371},{\"id\":\"112430105000000\",\"name\":\"开福区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371},{\"id\":\"112430111000000\",\"name\":\"雨花区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371},{\"id\":\"112430112000000\",\"name\":\"望城区\",\"num\":\"0\",\"aircraftNames\":[],\"capacity\":null,\"time\":1722659371}]";
        List<SectorStatVo>  models=JSONArray.parseArray(defaultData, SectorStatVo.class);
        models.forEach(src -> {
            src.setTime(System.currentTimeMillis());
        });
        return Result.createSuccess(models);
    }

}
